package com.nondo.rdp.finance.repository.course;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.finance.domain.course.CourseBuy;

public interface CourseBuyRepository extends JpaRepository<CourseBuy, Long>,
		JpaSpecificationExecutor<CourseBuy> {
	@Query("select a from CourseBuy a where a.id=:id and delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
	Optional<CourseBuy> findOne(@Param("id") int id);

	@Modifying
	@Query("update CourseBuy set delFlag='" + StatusConstant.DEL_FLAG_DELETE + "' where id = ?1")
	void deleteById(Long id);

	@SuppressWarnings("unchecked")
	CourseBuy save(CourseBuy courseBuy);

	@Query("select a from CourseBuy a where a.course.id=:cid and a.user.id=:uid and status = " + StatusConstant.COURSE_PAY_SUCCESS + " and delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
	CourseBuy getOne(@Param("cid") Long cid, @Param("uid") Long uid);

	CourseBuy findByOrderNo(String mchOrderNo);

}
